package cn.iocoder.yudao.module.system.dal.mysql.topscorerecord;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.dal.dataobject.topscorerecord.TopScoreRecordDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 最高分记录 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface TopScoreRecordMapper extends BaseMapperX<TopScoreRecordDO> {

    default PageResult<TopScoreRecordDO> selectPage(PageParam pageParam, Long examId, Long studentId, Integer rank) {
        return selectPage(pageParam, new LambdaQueryWrapperX<TopScoreRecordDO>()
                .eqIfPresent(TopScoreRecordDO::getExamId, examId)
                .eqIfPresent(TopScoreRecordDO::getStudentId, studentId)
                .eqIfPresent(TopScoreRecordDO::getRank, rank)
                .orderByAsc(TopScoreRecordDO::getRank)
                .orderByDesc(TopScoreRecordDO::getOriginalScore));
    }

    default List<TopScoreRecordDO> selectList(Long examId, Long studentId, Integer rank) {
        return selectList(new LambdaQueryWrapperX<TopScoreRecordDO>()
                .eqIfPresent(TopScoreRecordDO::getExamId, examId)
                .eqIfPresent(TopScoreRecordDO::getStudentId, studentId)
                .eqIfPresent(TopScoreRecordDO::getRank, rank)
                .orderByAsc(TopScoreRecordDO::getRank)
                .orderByDesc(TopScoreRecordDO::getOriginalScore));
    }

    default List<TopScoreRecordDO> selectListByExamId(Long examId) {
        return selectList(new LambdaQueryWrapperX<TopScoreRecordDO>()
                .eq(TopScoreRecordDO::getExamId, examId)
                .orderByAsc(TopScoreRecordDO::getRank)
                .orderByDesc(TopScoreRecordDO::getOriginalScore));
    }

    default List<TopScoreRecordDO> selectListByStudentId(Long studentId) {
        return selectList(new LambdaQueryWrapperX<TopScoreRecordDO>()
                .eq(TopScoreRecordDO::getStudentId, studentId)
                .orderByAsc(TopScoreRecordDO::getRank));
    }

    default TopScoreRecordDO selectByExamIdAndStudentIdAndRank(Long examId, Long studentId, Integer rank) {
        return selectOne(new LambdaQueryWrapperX<TopScoreRecordDO>()
                .eq(TopScoreRecordDO::getExamId, examId)
                .eq(TopScoreRecordDO::getStudentId, studentId)
                .eq(TopScoreRecordDO::getRank, rank));
    }

    default List<TopScoreRecordDO> selectTopScoresByExamAndSubject(Long examId, String subjectName, Integer limit) {
        return selectList(new LambdaQueryWrapperX<TopScoreRecordDO>()
                .eq(TopScoreRecordDO::getExamId, examId)
                .eq(TopScoreRecordDO::getSubjectName, subjectName)
                .orderByDesc(TopScoreRecordDO::getOriginalScore)
                .last("LIMIT " + limit));
    }

    /**
     * 根据考试ID删除所有最高分记录（用于清除导入的数据）
     */
    default int deleteByExamId(Long examId) {
        return delete(new LambdaQueryWrapperX<TopScoreRecordDO>()
                .eq(TopScoreRecordDO::getExamId, examId));
    }

    /**
     * 批量删除最高分记录（根据考试ID和学生ID列表）
     */
    default int deleteByExamIdAndStudentIds(Long examId, List<Long> studentIds) {
        if (studentIds == null || studentIds.isEmpty()) {
            return 0;
        }
        return delete(new LambdaQueryWrapperX<TopScoreRecordDO>()
                .eq(TopScoreRecordDO::getExamId, examId)
                .in(TopScoreRecordDO::getStudentId, studentIds));
    }
    
    /**
     * 根据导入批次ID删除最高分记录
     */
    default int deleteByImportBatchId(String importBatchId) {
        return delete(new LambdaQueryWrapperX<TopScoreRecordDO>()
                .eq(TopScoreRecordDO::getImportBatchId, importBatchId));
    }
    
    /**
     * 根据导入批次ID查询最高分记录数量
     */
    default Long selectCountByImportBatchId(String importBatchId) {
        return selectCount(TopScoreRecordDO::getImportBatchId, importBatchId);
    }

}